﻿using System.Reactive;
using System.Reactive.Linq;
using System.Windows.Media;
using CommonEntity.CommonImageEntities;
using OpenCvSharp;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ScienceAppService.Entities;
using ScienceAppService.ModuleInterface;
using Point3d = OpenCvSharp.Point3d;

namespace ScienceAIApp.ViewModels.ToolBarPanel;
public class DrawBrushViewModel : ScienceBaseViewModel
{
    private IMarkDrawService _markDrawService;

    private IImageShowService _imageShowService;

    [Reactive] public Mat BorderThicknessMat { get; set; }

    [Reactive] public int BorderThickness { get; set; }

    [Reactive] public int SelectedDrawTypeIndex { get; set; }

    [Reactive] public bool IsShowDrawPanel { get; set; }

    public SegToolsEnum CurrenToolsEnum { get; set; }

    [Reactive] public SolidColorBrush SelectedBrush { get; set; }

    /// <summary>
    /// 画刷 粗细调节
    /// </summary>
    public ReactiveCommand<bool, Unit> AdjustBrushThincknessCommand;

    /// <summary>
    /// 保存勾画信息
    /// </summary>
    public ReactiveCommand<Unit, Unit> SaveDrawCommand;


    public DrawBrushViewModel(IMarkDrawService markDrawService, IImageShowService imageShowService)
    {
        SelectedDrawTypeIndex = 0;

        _markDrawService = markDrawService;

        BorderThickness = 5;

        _imageShowService = imageShowService;

        IsShowDrawPanel = true;

        BorderThicknessMat = _markDrawService.SetBrushThickness(BorderThickness)!;

        // 标注类型
        this.WhenAnyValue(t => t.SelectedDrawTypeIndex).Do(si =>
        {

            switch (si)
            {
                case 0:
                    CurrenToolsEnum = SegToolsEnum.DrawNone;
                    _markDrawService.SetMarkOrDrawType(SegToolsEnum.DrawNone);
                    SelectedBrush = Brushes.LightSeaGreen;
                    break;
                case 1:
                    CurrenToolsEnum = SegToolsEnum.DrawULNA;
                    SelectedBrush = Brushes.IndianRed;
                    _markDrawService.SetMarkOrDrawType(SegToolsEnum.DrawULNA);
                    break;
                case 2:
                    CurrenToolsEnum = SegToolsEnum.DrawRADIUS;
                    SelectedBrush = Brushes.Yellow;
                    _markDrawService.SetMarkOrDrawType(SegToolsEnum.DrawRADIUS);
                    break;
                case 3:
                    CurrenToolsEnum = SegToolsEnum.DrawTFCC;
                    SelectedBrush = Brushes.LightGreen;
                    _markDrawService.SetMarkOrDrawType(SegToolsEnum.DrawTFCC);
                    break;
            }

            BorderThicknessMat = _markDrawService.SetBrushThickness(BorderThickness)!;

        }).Subscribe();

        // 绘制画笔调整
        AdjustBrushThincknessCommand = ReactiveCommand.Create<bool>(flag =>
        {
            if (flag)
            {
                if (BorderThickness < 18)
                    BorderThickness++;
            }
            else
            {
                if (BorderThickness > 1)
                    BorderThickness--;
            }

            BorderThicknessMat = _markDrawService.SetBrushThickness(BorderThickness)!;

        });

        SaveDrawCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            await _markDrawService.SaveDrawMat();
        });

    }


}
